//
//  SelectProductViewController.m
//  功能模块代码
//
//  Created by apple  on 2017/10/31.
//  Copyright © 2017年 JMZ. All rights reserved.
//

#import "SelectProductViewController.h"

@interface SelectProductViewController ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UITextField *inputField;

@property (nonatomic, strong) SelectPhotoUtil *selectUtil;

@end

@implementation SelectProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *takePhoto = [UIButton buttonWithType:UIButtonTypeCustom];
    [takePhoto setTitle:@"点击拍照" forState:UIControlStateNormal];
    takePhoto.backgroundColor = [UIColor orangeColor];
    [takePhoto addTarget:self action:@selector(TakePhotoAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:takePhoto];
    [takePhoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(80);
        make.leftMargin.offset(20);
        make.width.offset(100);
        make.height.offset(40);
    }];
    
    _imageView = [[UIImageView alloc] init];
    [self.view addSubview:_imageView];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(takePhoto.mas_bottom).offset(20);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.offset(ScreenW - 40);//w:h = 4:3
        make.height.offset(ScreenW - 40);
    }];
    
    UILabel *location = [[UILabel alloc] init];
    location.text = @"定位到的位置";
    [self.view addSubview:location];
    [location mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_imageView.mas_bottom).offset(10);
        make.height.offset(20);
        make.leadingMargin.offset(10);
    }];
    
    _inputField = [[UITextField alloc] init];
    _inputField.placeholder = @"我来发表几句:";
    [self.view addSubview:_inputField];
    [_inputField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(location.mas_bottom).offset(10);
        make.height.offset(50);
        make.leftMargin.offset(10);
        make.rightMargin.offset(-10);
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)TakePhotoAction:(UIButton *)sender
{
    _selectUtil = [SelectPhotoUtil sharedSelectPhotoUtil];
    [_selectUtil showSelectPhotoView:self allowsEdit:YES];
    
    __weak typeof (self) weakSelf = self;
    self.selectUtil.choosePhotoFinish = ^(void){
        weakSelf.imageView.image = [weakSelf addImage:[weakSelf imageWithView] toImage:weakSelf.selectUtil.selectImage];
    };
}

//测试图片合成 将view合成到Image上
- (UIImage *)imageWithView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _imageView.mj_size.width, 150)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, _imageView.mj_size.width, 40)];
    title.text = @"分享5人，立减10元";
    title.textColor = [UIColor redColor];
    title.font = [UIFont systemFontOfSize:30];
    [view addSubview:title];
    
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return img;
}

-(UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2
{
    UIGraphicsBeginImageContext(image2.size);
    
    //Draw image2
    [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
    
    //Draw image1
    [image1 drawInRect:CGRectMake(100, 200, image1.size.width, image1.size.height)];
    
    UIImage *resultImage=UIGraphicsGetImageFromCurrentImageContext();
    
    UIImageWriteToSavedPhotosAlbum(resultImage, self, nil, nil);
    UIGraphicsEndImageContext();
    
    return resultImage;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
